package com.uusee.crawler.job;

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;

import org.apache.commons.lang.time.StopWatch;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.uusee.crawler.fetcher.FetchHTTP;
import com.uusee.crawler.fetcher.FetchImage;
import com.uusee.crawler.model.CrawlStatusCodes;
import com.uusee.crawler.model.CrawlURI;
import com.uusee.crawler.pageprocessor.baike.imdb.ImdbPosterPageProcessor;
import com.uusee.crawler.util.PathUtils;
import com.uusee.shipshape.bk.vo.Poster;

public class ImdbPosterCrawlJob {
	private Log log = LogFactory.getLog(MtimeImagesCrawlJob.class);
	private FetchHTTP fetch = new FetchHTTP();
	private FetchImage imageFetch = new FetchImage();
	private ImdbPosterPageProcessor pageProcessor = new ImdbPosterPageProcessor();
	
	private int start = 2198;
	private int end =   46126;
	
	public static void main(String[] args) {
		try {
			//ApplicationContext acx = new ClassPathXmlApplicationContext("spring/applicationContext-framework.xml");
			ImdbPosterCrawlJob job = new ImdbPosterCrawlJob();
			//UniversalBo universalBo = (UniversalBo)acx.getBean("bo");
			
			job.doExecute();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void doExecute() {
		try {
			StopWatch stopWatch = new StopWatch();
			stopWatch.start();
			
			//抓取
			for (int id = start; id <=end; id++) {
				try {
					log.info("www.imdb.cn-抓取海报:"+id);
					crawl(createCrawlURI(id+""));
					Thread.sleep(5000);
				} catch (Exception e) {
					e.printStackTrace();
				}
				if (id%100==0 && id!=start) {
					log.info("www.imdb.cn-抓取海报数:"+(id-start)+";用时:"+stopWatch.getTime()/1000+"秒");
				}
			}
			stopWatch.stop();
			log.info("抓取结束,用时：" + stopWatch.getTime() / 1000 + "秒。");
		} catch (Exception e) {
			log.error(e);
		}
	}
	
	public CrawlURI createCrawlURI(String logoId) {
		CrawlURI crawlURI = new CrawlURI();
		String crawlUrl = "http://posters.imdb.cn/poster/"+logoId;
		crawlURI.setSourceSite("www.imdb.cn");
		crawlURI.setCrawlUrl(crawlUrl);
		crawlURI.setCharset("gb2312");
		return crawlURI;
	}
	
	public void crawl(CrawlURI crawlURI) {
		BufferedOutputStream osw = null;
		try {
			fetch.process(crawlURI);
			pageProcessor.process(crawlURI);
			if (crawlURI.getCrawlStatus() == CrawlStatusCodes.PAGE_PROCESS_SUCCESS) {
				Poster p = (Poster)crawlURI.getModel();
				
				CrawlURI posterCrawlURI = new CrawlURI();
				posterCrawlURI.setCrawlUrl(p.getUrl());
				imageFetch.process(posterCrawlURI);
				if (posterCrawlURI.getCrawlStatus() == CrawlStatusCodes.FETCH_SUCCESS) {
					byte[] crawlBytes = posterCrawlURI.getCrawlBytes();
					
					//图片保存路径
					StringBuilder imagePathSb = new StringBuilder();
					imagePathSb.append("d:\\data0\\output\\");
					imagePathSb.append(p.getSourceSite());
					imagePathSb.append("\\");
					imagePathSb.append("poster");
					imagePathSb.append("\\");
					imagePathSb.append(PathUtils.getContainIdSubPathById(p.getOriId()));
					File imagePath = new File(imagePathSb.toString());
					if (!imagePath.exists()) {
						imagePath.mkdirs();
					}
					
					//生成LOGO
					File posterFile = new File(imagePath,p.getName()+"_"+p.getId()+".jpg");
					FileOutputStream fio = new FileOutputStream(posterFile);
					osw = new BufferedOutputStream(new DataOutputStream(fio));

					osw.write(crawlBytes, 0, crawlBytes.length);
					osw.flush();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (osw!=null) {
				try {
					osw.close();
				}catch(Exception e){
					
				}
			}
		}
	}
	
}
